package components
{
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.TextInput;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class GlavniPanel{
		private var panel:SuperPanel = new SuperPanel();
		private var label1:Label = new Label();
		private var logOut:Label = new Label();
		private var settings:Label = new Label();
		private var chat:Label = new Label();
		private var alati:Label = new Label();
		private var poruke:Label = new Label();
		private var courses:Label = new Label();
		private var username:Label = new Label();
		private var tip:Label = new Label();
		private var privateMessages:Label = new Label();
		private var resources:Label = new Label();
		private var resourcesInput:TextInput = new TextInput();
		private var avatar:Image = new Image();
		private var tool:Tool = new Tool();
		private var btn:Button = new Button();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		private var service3:Mysql = new Mysql();
		private var chatComponent:Chat;
		private var allCourses:AllCourses;
		private var myResources:MyResources;
		
		public function GlavniPanel(p:SuperPanel){
			panel = p;
			label1.x = 10;
			label1.y = 27;
			label1.width = panel.width-30;
			label1.height = 30;
			username.x = 10;
			username.y = 55;
			username.width = panel.width-30;
			username.height = 20;
			username.htmlText = "Korisnik: <b>"+panel.parentApplication.user+"</b>";
			tip.x = 10;
			tip.y = 68;
			tip.width = panel.width-30;
			tip.height = 20;
			var tipText:String ="";
			if(panel.parentApplication.tip==0) tipText = "Administrator";
			if(panel.parentApplication.tip==1) tipText = "Student";
			if(panel.parentApplication.tip==2) tipText = "Profesor"; 
			tip.htmlText = "Tip korisnika: <b>"+tipText +"</b>";
			privateMessages.x = 10;
			privateMessages.y = 81;
			privateMessages.width = panel.width-30;
			privateMessages.height = 20;
			privateMessages.htmlText = "Novih privatnih poruka: <b></b>";
			resources.x = 10;
			resources.y = 205;
			resources.height = 20;
			resources.htmlText = "Otvori resurs";
			resourcesInput.x = 87;
			resourcesInput.y = 207;
			resourcesInput.width = panel.width-149;
			resourcesInput.maxChars = 5;
			resourcesInput.height = 15;
			btn.x = 140;
			btn.y = 207;
			btn.width = 40;
			btn.height = 15;
			btn.label = "...";
			btn.buttonMode = true;
			btn.useHandCursor = true;
			logOut.htmlText = "<b>Izlaz</b>";
			logOut.buttonMode = true;
			logOut.useHandCursor = true;
			logOut.mouseChildren = false;
			logOut.x = 110;
			logOut.y = panel.height-40;
			logOut.height = 17;
			settings.htmlText = "<b>Podesavanja ::</b>";
			settings.buttonMode = true;
			settings.useHandCursor = true;
			settings.mouseChildren = false;
			settings.x = 20;
			settings.y = panel.height-40;
			settings.height = 17;
			poruke.htmlText = "<b>Poruke</b>";
			poruke.buttonMode = true;
			poruke.useHandCursor = true;
			poruke.mouseChildren = false;
			poruke.x = 110;
			poruke.y = panel.height-50;
			poruke.height = 17;
			courses.htmlText = "    <b>     Kursevi ::</b>";
			courses.buttonMode = true;
			courses.useHandCursor = true;
			courses.mouseChildren = false;
			courses.x = 20;
			courses.y = panel.height-50;
			courses.height = 17;
			chat.htmlText = "<b>Chat</b>";
			chat.buttonMode = true;
			chat.useHandCursor = true;
			chat.mouseChildren = false;
			chat.x = 110;
			chat.y = panel.height-60;
			chat.height = 17;
			alati.htmlText = "  <b>Moji resursi ::</b>";
			alati.buttonMode = true;
			alati.useHandCursor = true;
			alati.mouseChildren = false;
			alati.x = 20;
			alati.y = panel.height-60;
			alati.height = 17;
			avatar.source = (panel.parentApplication.avatar!="") ? panel.parentApplication.AVATAR_PATH+panel.parentApplication.avatar : panel.parentApplication.AVATAR_PATH+"nophoto.jpg";
			avatar.x = 50;
			avatar.y = 100;
			avatar.width = 100;
			avatar.height = 100;
			label1.htmlText = "Zadnja posjeta : <b>"+panel.parentApplication.last_login_date+"</b>\n                        <b>"+panel.parentApplication.last_login_time+"</b>";
			panel.addChild(username);
			panel.addChild(resources);
			panel.addChild(resourcesInput);
			panel.addChild(tip);
			panel.addChild(btn);
			panel.addChild(chat);
			panel.addChild(alati);
			panel.addChild(label1);
			panel.addChild(logOut);
			panel.addChild(settings);
			panel.addChild(avatar);
			panel.addChild(privateMessages);
			panel.addChild(poruke);
			panel.addChild(courses);
			
			settings.addEventListener(MouseEvent.MOUSE_OVER, settingsOver);
			settings.addEventListener(MouseEvent.MOUSE_OUT, settingsOut);
			settings.addEventListener(MouseEvent.CLICK, settingsClick);
			logOut.addEventListener(MouseEvent.MOUSE_OVER, logOutOver);
			logOut.addEventListener(MouseEvent.MOUSE_OUT, logOutOut);
			logOut.addEventListener(MouseEvent.CLICK, logOutClick);
			poruke.addEventListener(MouseEvent.MOUSE_OVER, porukeOver);
			poruke.addEventListener(MouseEvent.MOUSE_OUT, porukeOut);
			poruke.addEventListener(MouseEvent.CLICK, porukeClick);
			courses.addEventListener(MouseEvent.MOUSE_OVER, coursesOver);
			courses.addEventListener(MouseEvent.MOUSE_OUT, coursesOut);
			courses.addEventListener(MouseEvent.CLICK, coursesClick);
			chat.addEventListener(MouseEvent.MOUSE_OVER, chatOver);
			chat.addEventListener(MouseEvent.MOUSE_OUT, chatOut);
			chat.addEventListener(MouseEvent.CLICK, chatClick);
			alati.addEventListener(MouseEvent.MOUSE_OVER, alatiOver);
			alati.addEventListener(MouseEvent.MOUSE_OUT, alatiOut);
			alati.addEventListener(MouseEvent.CLICK, alatiClick);
			btn.addEventListener(MouseEvent.CLICK, openResource);
			refreshNewMessage();
		}
		
		public function refreshNewMessage():void{
			service1.sql = {type:"SELECT", a1:"p.primaoc", a2:"COUNT(p.id_poruka)", from:"privatnaporuka p", where:"p.procitana=0 AND p.primaoc="+panel.parentApplication.username+" GROUP BY p.primaoc" };
	 		service1.addEventListener(service1.MYSQL_RESULT, onNewMessage);
	 		service1.startService();
		}

		public function onNewMessage(e:Event):void{
			var xmlList:XMLList = service1.xml.children();
			if(xmlList.length()>0) privateMessages.htmlText = "Novih privatnih poruka: <b>"+service1.xml.result.COUNT_p_id_poruka_+"</b>";
				else privateMessages.htmlText = "Novih privatnih poruka: <b>0</b>";
		}
		
		public function openResource(e:MouseEvent):void{
			if(resourcesInput.text.length>0){
				service.sql = {type:"SELECT", a1:"r.id_resurs", a2:"r.url", a3:"r.user", a4:"k.username", a5:"t.tip", a6:"t.program", a7:"r.sifra", a8:"r.naziv", a9:"r.opis", a10:"r.datum", a11:"r.vrijeme", from:"resursi r, korisnik k, tipovi t", where:"r.tip=t.id_tip AND r.user=k.id_username AND r.id_resurs="+resourcesInput.text };
	 			service.addEventListener(service.MYSQL_RESULT, onOpenResource);
	 			service.startService();
	 		} else tool.createAlert("Greska!", "Morate upisati id resursa.");	
		}
		
		public function onOpenResource(e:Event):void{
			var xmlList:XMLList = service.xml.children();
			if(xmlList.length()>0){
				if(!panel.parentApplication.checkExist("Resurs("+xmlList.id_resurs+")")){
					var s:SuperPanel;
					panel.parentApplication.panels.push(s = tool.createPanel("Resurs("+xmlList.id_resurs+")", 0, 330, 280, 300, 150, true, false, "Resurs("+xmlList.id_resurs+")"));
					panel.parentApplication.addChild(s);
					s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
					s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
					var resurs:Resource = new Resource(s);
					resurs.show(xmlList.id_resurs, xmlList.url, xmlList.tip, xmlList.user, xmlList.username, xmlList.program, xmlList.sifra, xmlList.naziv, xmlList.opis, xmlList.datum, xmlList.vrijeme);
					if(xmlList.sifra!="") {
						resurs.sifrirano = true;				
					} else resurs.sifrirano = false;
				}
			} else tool.createAlert("Greska!", "Resurs sa ukucanim id-om ne postoji!");
		}
		
		public function settingsOver(e:MouseEvent):void{
			settings.htmlText = "<b><u>Podesavanja</u> ::</b>";
		}
		
		public function settingsOut(e:MouseEvent):void{
			settings.htmlText = "<b>Podesavanja ::</b>";
		}

		public function settingsClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("PODESAVANJA")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("PODESAVANJA", 0, 330, 280, 300, 150, true, false, "Podesavanja"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var podesavanja:Settings = new Settings(s);
			}
		}
		
		public function logOutClick(e:MouseEvent):void{
			panel.parentApplication.logOut();
		}

		public function logOutOver(e:MouseEvent):void{
			logOut.htmlText = "<b><u>Izlaz</u></b>";
		}
		
		public function logOutOut(e:MouseEvent):void{
			logOut.htmlText = "<b>Izlaz</b>";
		}
		
		public function chatClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("CHAT - Glavni kanal")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("CHAT - Glavni kanal", 0, 330, 280, 300, 150, true, true, "CHAT"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				chatComponent = new Chat(s);
				chatComponent.sifrirano = false;
				chatComponent.room = 0;
				service2.sql = {type:"SELECT", a1:"s.id_soba", a2:"s.naziv", a3:"s.sifra", from:"soba s", where:"" };
	 			service2.addEventListener(service2.MYSQL_RESULT, onRoomsLoaded);
	 			service2.startService();
			}
		}
		
		public function onRoomsLoaded(e:Event):void{
			chatComponent.setRooms(service2.xml);
		}

		public function chatOver(e:MouseEvent):void{
			chat.htmlText = "<b><u>Chat</u></b>";
		}
		
		public function chatOut(e:MouseEvent):void{
			chat.htmlText = "<b>Chat</b>";
		}
		
		public function coursesClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("Svi kursevi")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("Svi kursevi", 0, 300, 250, 300, 0, true, true, "SviKursevi"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				allCourses = new AllCourses(s);
     			service3.sql = {type:"SELECT", a1:"k.naziv", a2:"k.id_kurs", a3:"k.sifra", a4:"k.opis", a5:"k.ects", from:"kurs k", where: "k.id_kurs>0 ORDER BY naziv ASC"};
	 			service3.addEventListener(service3.MYSQL_RESULT, onCoursesResult);
	 			service3.startService();
			}
		}
		
		public function onCoursesResult(e:Event):void{
			var xmlList:XMLList = service3.xml.children();
			var xmlPom:String = "<root naziv='Kursevi' tip='-1'>";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom+="<kurs ects='"+xmlList[i].ects+"' sifra='"+xmlList[i].sifra+"' opis='"+xmlList[i].opis+"' naziv='"+xmlList[i].naziv+"' id_kurs='"+xmlList[i].id_kurs+"' />";
			}
			xmlPom+="</root>";
			var xml:XML=new XML(xmlPom);
			allCourses.setDataProvider(xml);
			service3.removeEventListener(service3.MYSQL_RESULT, onCoursesResult);
		}

		public function coursesOver(e:MouseEvent):void{
			courses.htmlText = "    <b>     <u>Kursevi</u> ::</b>";
		}
		
		public function coursesOut(e:MouseEvent):void{
			courses.htmlText = "    <b>     Kursevi ::</b>";
		}
		
		public function alatiClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("Moji resursi")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("Moji resursi", 0, 300, 250, 300, 0, true, false, "MojiResursi"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				myResources = new MyResources(s);
			}
		}

		public function alatiOver(e:MouseEvent):void{
			alati.htmlText = "  <b><u>Moji resursi</u> ::</b>";
		}
		
		public function alatiOut(e:MouseEvent):void{
			alati.htmlText = "  <b>Moji resursi ::</b>";
		}
		
		public function porukeClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("PRIVATNE PORUKE")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("PRIVATNE PORUKE", 0, 330, 280, 300, 150, true, false, "PrivatnePoruke"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var poruke:Messages = new Messages(s);
			}
		}

		public function porukeOver(e:MouseEvent):void{
			poruke.htmlText = "<b><u>Poruke</u></b>";
		}
		
		public function porukeOut(e:MouseEvent):void{
			poruke.htmlText = "<b>Poruke</b>";
		}
		
		public function refreshAvatar(s:String):void{
			avatar.source = panel.parentApplication.AVATAR_PATH+s;
		}
	}
}